package org.zoomdev.zoom.aop.factory;

import org.zoomdev.zoom.aop.Interceptor;
import org.zoomdev.zoom.aop.annotations.Aop;
import org.zoomdev.zoom.common.exceptions.ZoomException;
import org.zoomdev.zoom.ioc.Ioc;

import java.lang.reflect.Method;
import java.util.List;

public final class AopInterceptorFactory extends AnnotationInterceptorFactory<Aop> {


    private final Ioc ioc;

    public AopInterceptorFactory(Ioc ioc) {
        this.ioc = ioc;
    }


    @Override
    protected void createInterceptor(Aop annotation, Class<?> targetClass, Method method, List<Interceptor> interceptors) {
        Class<? extends Interceptor>[] classOfMethodInterceptor = annotation.value();

        for (Class<? extends Interceptor> clazz : classOfMethodInterceptor) {
            try {
                interceptors.add(ioc.fetch(clazz));
            } catch (Exception e) {
                throw new ZoomException(String.format("在初始化MethodInterceptor[%s]的时候发生异常", clazz), e);
            }
        }
    }
}
